package com.cookbook.cookbookservice.controller;

import com.cookbook.cookbookservice.model.User;
import com.cookbook.cookbookservice.service.UserService;
import com.cookbook.cookbookservice.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public Response loginUser(String unionid, String token, long timeout) {
        User login = userService.login(unionid, token, timeout);
        if (login != null) {
            return Response.success(login);
        } else {
            return Response.error(4000, "登陆失败");
        }
    }

    @GetMapping("/getUser")
    public Response getUser(String token) {
        User user = userService.getUser(token);
        if (user == null) {
            return Response.error(4000, "未登录");
        }
        return Response.success(user);
    }

    @PostMapping("/update")
    public Response getUser(@RequestBody User user) {
        User user1 = userService.updateUser(user);
        if (user1 == null) {
            return Response.error(4001, "修改信息失败");
        }
        return Response.success(user1);
    }
}
